library(networkD3)
library(sp)
library(modelsummary)
library(readxl)
library(ggrepel)
library(fixest)
library(doParallel)
library(scales)
library(ggeffects)
library(did)
library(wru)
library(ebal)
library(tidycensus)
library(tidyverse)
library(data.table)
library(gridExtra)
library(kableExtra)
library(birdie)
library(cvap)

save <- c("cleanup", "theme_bc", "save", "out", "theme_bc")
options("modelsummary_format_numeric_latex" = "plain")

theme_bc <- function(base_size = 11, base_family = "",
                     legend.position = "right", face = "plain", ...) {
  library(extrafont)
  library(ggtext)
  half_line <- base_size/2
  theme_bw(base_family = base_family) %+replace%
    theme(plot.caption = element_textbox_simple(size = rel(0.8), hjust = 0,
                                                family = base_family,
                                                vjust = 1, margin = margin(t = half_line)),
          plot.title = element_text(size = rel(1.2), hjust = 0.5,
                                    vjust = 1, margin = margin(b = half_line),
                                    family = base_family),
          plot.subtitle = element_text(hjust = 0.5, vjust = 1, margin = margin(b = half_line)),
          legend.position = legend.position,
          text = element_text(family = base_family, face = face,
                              colour = "black", size = base_size, lineheight = 0.9,
                              hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(),
                              debug = FALSE),
          plot.caption.position =  "plot",
          ...)
}

cleanup <- function(...){
  save2 <- c(save, ...)
  rm(list=ls(envir = .GlobalEnv)[! ls(envir = .GlobalEnv) %in% save2], envir = .GlobalEnv)
  gc()
}

out <- "Output/"

options(modelsummary_factory_default = "kableExtra")
options(modelsummary_factory_latex = "kableExtra")
